<template>
  <div class="_flex-y _p20 _flex-a-stretch">
    <div class="_cart _p5">
      <el-button type="primary" @click="openSyncScene({})" size="small"
        >添加</el-button
      >
    </div>
    <div class="_flex _flex-wrap _flex-start _m-t-5">
      <div
        class="_cart _p5 _m-r-5 _m-b-5"
        v-for="item in sceneList"
        :key="item.id"
      >
        <div class="_font-title">{{ item.name }}</div>
        <div>{{ $tool.dateStr(item.created_date) }}</div>
        <div class="_flex _m-t-5">
          <el-button type="primary" size="small" @click="openViewScene(item)"
            >详情</el-button
          >
          <el-button type="warning" size="small" @click="openSyncScene(item)"
            >编辑</el-button
          >
          <el-button type="danger" size="small" @click="delScene(item.id)"
            >删除</el-button
          >
        </div>
      </div>
    </div>
    <ComDataUpdateDrawer
      ref="ComDataUpdateDrawer"
      code="things_scene"
      :currentForm="syncScene"
      @syncClose="initScene"
    />
    <el-drawer v-model="drawerView">
      <ComSceneView v-if="syncScene.id" :sceneId="syncScene.id"></ComSceneView>
    </el-drawer>
  </div>
</template>
<script>
import toolAction from "@/tool/http/tool-action";
import ComDataUpdateDrawer from "@/views/common/com-data-update-drawer.vue";
import ComSceneView from "./com-scene-view.vue";

let thingsSceneApi = toolAction.init("things_scene");

export default {
  components: { ComDataUpdateDrawer, ComSceneView },
  data() {
    return {
      sceneList: [],
      syncScene: {},
      drawerView: false,
    };
  },
  mounted() {
    this.initScene();
  },
  methods: {
    initScene() {
      thingsSceneApi.list().then((res) => {
        this.sceneList = res;
      });
    },
    openViewScene(syncScene) {
      this.syncScene = {};
      this.$nextTick(() => {
        this.syncScene = syncScene;
        this.drawerView = true;
      });
    },
    openSyncScene(syncScene) {
      this.syncScene = syncScene;
      this.$refs.ComDataUpdateDrawer.open();
    },
    delScene(id) {
      thingsSceneApi.remove({ id }).then(() => {
        this.$element.msg();
        this.initScene();
      });
    },
  },
};
</script>
<style lang=""></style>
